package com.jingzhongbao.springboot.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jingzhongbao.springboot.dao.UserRepository;
import com.jingzhongbao.springboot.domain.User;

@Controller
@RequestMapping(value="/user")
public class UserController {
	@Autowired
	private UserRepository userRepository;
	
	@ApiOperation(value="获取用户列表", notes="")
	@SuppressWarnings("deprecation")
	@RequestMapping(value={"/list"}, method=RequestMethod.GET)
	public String  getList(Model model) {
		int page=0,size=10;
	    Sort sort = new Sort(Direction.DESC, "id");
	    Pageable pageable = new PageRequest(page, size, sort);
		model.addAttribute("users", userRepository.findAll(pageable));
		return "/user/list";
	}
	
	@ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value="", method=RequestMethod.POST)
    public String postUser(@RequestBody User user) {
		userRepository.save(user);
        return "success";
    }
	@ApiOperation(value="获取用户详细信息", notes="根据url的username来获取用户详细信息")
    @ApiImplicitParam(name = "username", value = "用户ID", required = true, dataType = "String")
    @RequestMapping(value="/get/{username}", method=RequestMethod.GET, produces = "application/json")
    public User getUser(@PathVariable String username) {
        return userRepository.findByUsername(username);
    }
}
